<?php

namespace app\common\service\driver;

use app\common\model\driving\Freight;
use think\cache\driver\Redis;
use think\Db;
use think\Exception;

/**
 * 围栏计费规则服务类
 **/
class FreightRuleService
{

    /**
     * 获取距离和金额
     *
     * @param mixed $order 订单信息
     * @return array
     **/
    public static function get_kl($order)
    {
        // 获取电子围栏
        $freight = Freight::with(['freightRule'])
            ->where('area_code', $order['start_area_id'])
            ->find();
        $freight = $freight->toArray();

        // 匹配到围栏计算规则
        $info = [];
        $time = date('H:i:s');
        foreach ($freight['freight_rule'] as $val) {
            if ($val['in_time_start'] < $time && $time <= $val['in_time_end']) {
                $info = $val;
            }
        }
        if (!$info) {
            except('未匹配到围栏计算规则');
        }

//        // 获取途径点坐标
//        $options = \think\Config::get('redis');
//        $redis = new Redis($options);
//        $key = 'waypoints_';
//        $list = $redis->handler()->lrange($key . $order['order_id'], 0, -1); //获取列表中所有的值
//
//        // 组成16个途径点坐标串
//        $size = 16;
//        if (count($list) > $size) {
//            $array = splitArray($list, $size);
//            $waypoints_data = [];
//            foreach ($array as $val) {
//                $waypoints_data[] = $val[0];
//            }
//            $waypoints_16 = implode(';', $waypoints_data);
//        } else {
//            $waypoints_16 = implode(';', $list);
//        }
//        $waypoints = implode(';', $list);
        $waypoints = '118.352522,35.101954;118.353156,35.10212;118.35064,35.10209;118.348531,35.102059;118.346128,35.102063;118.345586,35.101256;118.345586,35.099707;118.345758,35.097503;118.346171,35.095971;118.346563,35.094141;118.346144,35.09235;118.345388,35.090402;118.344766,35.088786;118.343408,35.083514;118.343585,35.081763;118.343719,35.080384;118.343875,35.079138;118.344057,35.077381;118.344336,35.074682;118.34453,35.072772;118.344653,35.071393;118.34469,35.068504;118.344717,35.066383;118.344626,35.065461;118.344417,35.063183;118.344256,35.060517;118.344047,35.057531;118.344218,35.055248;118.344535,35.051173;118.344336,35.046816;118.34417,35.043386;118.344031,35.041379;118.343843,35.039583;118.343789,35.038669;118.343768,35.038524';
        $waypoints_16 = '118.352522,35.101954;118.348531,35.102059;118.345586,35.099707;118.346563,35.094141;118.345388,35.090402;118.343408,35.083514;118.343719,35.080384;118.344057,35.077381;118.34453,35.072772;118.34469,35.068504;118.344626,35.065461;118.344256,35.060517;118.344218,35.055248;118.344336,35.046816;118.344031,35.041379;118.343789,35.038669';

        // TODO 20240711
        // 驾车路线规划
        try{
            $api = new \amap\restapi();
            $result = $api->direction_driving($order['start_lng'] . ',' . $order['start_lat'], $order['end_lng'] . ',' . $order['end_lat'], 32, $waypoints_16);
        } catch (Exception $e){
            except($e->getMessage());
        }
        $json = json_encode($result,JSON_UNESCAPED_UNICODE);

//        $result = '{"status":"1","info":"OK","infocode":"10000","count":"1","route":{"origin":"118.351681,35.101445","destination":"118.342530,35.036830","taxi_cost":"16","paths":[{"distance":"8528","restriction":"0","cost":{"duration":"1590","tolls":"0","toll_distance":"0","traffic_lights":"20"},"steps":[{"instruction":"向西行驶31米右转","orientation":"西","step_distance":"31","cost":{"duration":"18","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"0"},"tmcs":[{"tmc_status":"未知","tmc_distance":"31","tmc_polyline":"118.35168,35.101545;118.351335,35.101543"}],"navi":{"action":"右转","assistant_action":""},"polyline":"118.35168,35.101545;118.351335,35.101543"},{"instruction":"向北行驶46米右转","orientation":"北","step_distance":"46","cost":{"duration":"32","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"0"},"tmcs":[{"tmc_status":"未知","tmc_distance":"12","tmc_polyline":"118.351335,35.101543;118.35134,35.101656"},{"tmc_status":"未知","tmc_distance":"34","tmc_polyline":"118.35134,35.101656;118.351346,35.101967"}],"navi":{"action":"右转","assistant_action":""},"polyline":"118.351335,35.101543;118.35134,35.101656;118.351346,35.101967"},{"instruction":"沿北京路向东行驶107米到达途经地","orientation":"东","road_name":"北京路","step_distance":"107","cost":{"duration":"11","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"0"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"43","tmc_polyline":"118.351346,35.101967;118.351818,35.101967"},{"tmc_status":"畅通","tmc_distance":"64","tmc_polyline":"118.351818,35.101967;118.352521,35.101967"}],"navi":{"action":"","assistant_action":"到达途经地"},"polyline":"118.351346,35.101967;118.351818,35.101967;118.352521,35.101967"},{"instruction":"沿北京路向东行驶64米左转调头","orientation":"东","road_name":"北京路","step_distance":"64","cost":{"duration":"42","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"0"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"64","tmc_polyline":"118.352521,35.101967;118.353228,35.101967"}],"navi":{"action":"左转调头","assistant_action":""},"polyline":"118.352521,35.101967;118.353228,35.101967"},{"instruction":"沿北京路向西行驶436米到达途经地","orientation":"西","road_name":"北京路","step_distance":"436","cost":{"duration":"58","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"1"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"14","tmc_polyline":"118.353228,35.101967;118.353218,35.102101"},{"tmc_status":"畅通","tmc_distance":"27","tmc_polyline":"118.353218,35.102101;118.352923,35.102106"},{"tmc_status":"畅通","tmc_distance":"100","tmc_polyline":"118.352923,35.102106;118.351823,35.102101"},{"tmc_status":"畅通","tmc_distance":"110","tmc_polyline":"118.351823,35.102101;118.350611,35.102096"},{"tmc_status":"畅通","tmc_distance":"46","tmc_polyline":"118.350611,35.102096;118.350106,35.102096"},{"tmc_status":"畅通","tmc_distance":"24","tmc_polyline":"118.350106,35.102096;118.349844,35.102096"},{"tmc_status":"畅通","tmc_distance":"16","tmc_polyline":"118.349844,35.102096;118.349726,35.10209;118.349656,35.10209"},{"tmc_status":"畅通","tmc_distance":"32","tmc_polyline":"118.349656,35.10209;118.349307,35.102096"},{"tmc_status":"畅通","tmc_distance":"67","tmc_polyline":"118.349307,35.102096;118.348567,35.102085"}],"navi":{"action":"","assistant_action":"到达途经地"},"polyline":"118.353228,35.101967;118.353218,35.102101;118.352923,35.102106;118.351823,35.102101;118.350611,35.102096;118.350106,35.102096;118.349844,35.102096;118.349726,35.10209;118.349656,35.10209;118.349307,35.102096;118.348567,35.102085"},{"instruction":"沿北京路向西行驶256米左转","orientation":"西","road_name":"北京路","step_distance":"256","cost":{"duration":"98","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"1"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"2","tmc_polyline":"118.348567,35.102085;118.348545,35.102085"},{"tmc_status":"畅通","tmc_distance":"53","tmc_polyline":"118.348545,35.102085;118.347961,35.10208"},{"tmc_status":"畅通","tmc_distance":"47","tmc_polyline":"118.347961,35.10208;118.34744,35.102074"},{"tmc_status":"畅通","tmc_distance":"87","tmc_polyline":"118.34744,35.102074;118.346475,35.10208"},{"tmc_status":"畅通","tmc_distance":"27","tmc_polyline":"118.346475,35.10208;118.346174,35.102074"},{"tmc_status":"畅通","tmc_distance":"40","tmc_polyline":"118.346174,35.102074;118.345735,35.102074"}],"navi":{"action":"左转","assistant_action":""},"polyline":"118.348567,35.102085;118.348545,35.102085;118.347961,35.10208;118.34744,35.102074;118.346475,35.10208;118.346174,35.102074;118.345735,35.102074"},{"instruction":"沿沂蒙北路向南行驶250米到达途经地","orientation":"南","road_name":"沂蒙北路","step_distance":"250","cost":{"duration":"30","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"0"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"47","tmc_polyline":"118.345633,35.101967;118.345627,35.101538"},{"tmc_status":"畅通","tmc_distance":"83","tmc_polyline":"118.345627,35.101538;118.345622,35.101318;118.3456,35.100781"},{"tmc_status":"畅通","tmc_distance":"95","tmc_polyline":"118.3456,35.100781;118.345573,35.099928"},{"tmc_status":"畅通","tmc_distance":"25","tmc_polyline":"118.345573,35.099928;118.345578,35.099706"}],"navi":{"action":"","assistant_action":"到达途经地"},"polyline":"118.345633,35.101967;118.345627,35.101538;118.345622,35.101318;118.3456,35.100781;118.345573,35.099928;118.345578,35.099706"},{"instruction":"沿沂蒙北路途径金锣大桥向南行驶623米到达途经地","orientation":"南","road_name":"沂蒙北路","step_distance":"623","cost":{"duration":"54","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"2"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"6","tmc_polyline":"118.345578,35.099706;118.345579,35.099655"},{"tmc_status":"畅通","tmc_distance":"48","tmc_polyline":"118.345579,35.099655;118.345584,35.099295;118.345584,35.09922"},{"tmc_status":"畅通","tmc_distance":"127","tmc_polyline":"118.345584,35.09922;118.345595,35.098652;118.345611,35.09833;118.345654,35.098083"},{"tmc_status":"畅通","tmc_distance":"99","tmc_polyline":"118.345654,35.098083;118.345804,35.097305;118.345831,35.097198"},{"tmc_status":"畅通","tmc_distance":"112","tmc_polyline":"118.345831,35.097198;118.346105,35.096205"},{"tmc_status":"畅通","tmc_distance":"111","tmc_polyline":"118.346105,35.096205;118.346346,35.095218"},{"tmc_status":"畅通","tmc_distance":"74","tmc_polyline":"118.346346,35.095218;118.346501,35.094553"},{"tmc_status":"畅通","tmc_distance":"35","tmc_polyline":"118.346501,35.094553;118.346555,35.094242"},{"tmc_status":"畅通","tmc_distance":"11","tmc_polyline":"118.346555,35.094242;118.346565,35.094141"}],"navi":{"action":"","assistant_action":"到达途经地"},"polyline":"118.345578,35.099706;118.345579,35.099655;118.345584,35.099295;118.345584,35.09922;118.345595,35.098652;118.345611,35.09833;118.345654,35.098083;118.345804,35.097305;118.345831,35.097198;118.346105,35.096205;118.346346,35.095218;118.346501,35.094553;118.346555,35.094242;118.346565,35.094141"},{"instruction":"沿金锣大桥向南行驶432米到达途经地","orientation":"南","road_name":"金锣大桥","step_distance":"432","cost":{"duration":"26","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"0"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"48","tmc_polyline":"118.346565,35.094141;118.346577,35.094017;118.346566,35.093711"},{"tmc_status":"畅通","tmc_distance":"384","tmc_polyline":"118.346566,35.093711;118.346501,35.093362;118.346384,35.092992;118.345799,35.091447;118.345391,35.0904"}],"navi":{"action":"","assistant_action":"到达途经地"},"polyline":"118.346565,35.094141;118.346577,35.094017;118.346566,35.093711;118.346501,35.093362;118.346384,35.092992;118.345799,35.091447;118.345391,35.0904"},{"instruction":"沿金锣大桥向南行驶623米靠左沿主路行驶","orientation":"南","road_name":"金锣大桥","step_distance":"623","cost":{"duration":"37","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"0"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"472","tmc_polyline":"118.345391,35.0904;118.345364,35.090331;118.344726,35.088685;118.343825,35.086356"},{"tmc_status":"畅通","tmc_distance":"74","tmc_polyline":"118.343825,35.086356;118.343701,35.086035;118.343578,35.085713"},{"tmc_status":"畅通","tmc_distance":"77","tmc_polyline":"118.343578,35.085713;118.343358,35.085037"}],"navi":{"action":"靠左","assistant_action":"沿主路行驶"},"polyline":"118.345391,35.0904;118.345364,35.090331;118.344726,35.088685;118.343825,35.086356;118.343701,35.086035;118.343578,35.085713;118.343358,35.085037"},{"instruction":"沿沂蒙路向南行驶169米到达途经地","orientation":"南","road_name":"沂蒙路","step_distance":"169","cost":{"duration":"23","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"0"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"120","tmc_polyline":"118.343358,35.085037;118.343331,35.084865;118.343315,35.08442;118.343358,35.083953"},{"tmc_status":"畅通","tmc_distance":"42","tmc_polyline":"118.343358,35.083953;118.343396,35.083572"},{"tmc_status":"畅通","tmc_distance":"7","tmc_polyline":"118.343396,35.083572;118.343401,35.083513"}],"navi":{"action":"","assistant_action":"到达途经地"},"polyline":"118.343358,35.085037;118.343331,35.084865;118.343315,35.08442;118.343358,35.083953;118.343396,35.083572;118.343401,35.083513"},{"instruction":"沿沂蒙路向南行驶345米到达途经地","orientation":"南","road_name":"沂蒙路","step_distance":"345","cost":{"duration":"67","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"2"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"12","tmc_polyline":"118.343401,35.083513;118.343412,35.083406"},{"tmc_status":"畅通","tmc_distance":"26","tmc_polyline":"118.343412,35.083406;118.343438,35.08317"},{"tmc_status":"畅通","tmc_distance":"31","tmc_polyline":"118.343438,35.08317;118.343465,35.082891"},{"tmc_status":"畅通","tmc_distance":"6","tmc_polyline":"118.343465,35.082891;118.343471,35.082837"},{"tmc_status":"畅通","tmc_distance":"29","tmc_polyline":"118.343471,35.082837;118.343503,35.082569"},{"tmc_status":"畅通","tmc_distance":"69","tmc_polyline":"118.343503,35.082569;118.343567,35.081947"},{"tmc_status":"畅通","tmc_distance":"43","tmc_polyline":"118.343567,35.081947;118.3436,35.08162;118.343605,35.081555"},{"tmc_status":"畅通","tmc_distance":"31","tmc_polyline":"118.343605,35.081555;118.343631,35.081266"},{"tmc_status":"畅通","tmc_distance":"64","tmc_polyline":"118.343631,35.081266;118.343691,35.080686"},{"tmc_status":"畅通","tmc_distance":"34","tmc_polyline":"118.343691,35.080686;118.343725,35.080384"}],"navi":{"action":"","assistant_action":"到达途经地"},"polyline":"118.343401,35.083513;118.343412,35.083406;118.343438,35.08317;118.343465,35.082891;118.343471,35.082837;118.343503,35.082569;118.343567,35.081947;118.3436,35.08162;118.343605,35.081555;118.343631,35.081266;118.343691,35.080686;118.343725,35.080384"},{"instruction":"沿沂蒙路向南行驶333米到达途经地","orientation":"南","road_name":"沂蒙路","step_distance":"333","cost":{"duration":"86","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"0"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"20","tmc_polyline":"118.343725,35.080384;118.343744,35.080209"},{"tmc_status":"畅通","tmc_distance":"49","tmc_polyline":"118.343744,35.080209;118.343803,35.079769"},{"tmc_status":"畅通","tmc_distance":"25","tmc_polyline":"118.343803,35.079769;118.343835,35.079538"},{"tmc_status":"畅通","tmc_distance":"32","tmc_polyline":"118.343835,35.079538;118.343851,35.079345;118.343862,35.079248"},{"tmc_status":"畅通","tmc_distance":"121","tmc_polyline":"118.343862,35.079248;118.343986,35.078165"},{"tmc_status":"畅通","tmc_distance":"17","tmc_polyline":"118.343986,35.078165;118.344002,35.078009"},{"tmc_status":"畅通","tmc_distance":"32","tmc_polyline":"118.344002,35.078009;118.344029,35.077714"},{"tmc_status":"畅通","tmc_distance":"37","tmc_polyline":"118.344029,35.077714;118.344055,35.07738"}],"navi":{"action":"","assistant_action":"到达途经地"},"polyline":"118.343725,35.080384;118.343744,35.080209;118.343803,35.079769;118.343835,35.079538;118.343851,35.079345;118.343862,35.079248;118.343986,35.078165;118.344002,35.078009;118.344029,35.077714;118.344055,35.07738"},{"instruction":"沿沂蒙路向南行驶510米到达途经地","orientation":"南","road_name":"沂蒙路","step_distance":"510","cost":{"duration":"73","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"1"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"40","tmc_polyline":"118.344055,35.07738;118.344071,35.077167;118.344093,35.077028"},{"tmc_status":"畅通","tmc_distance":"23","tmc_polyline":"118.344093,35.077028;118.344115,35.076813"},{"tmc_status":"畅通","tmc_distance":"21","tmc_polyline":"118.344115,35.076813;118.344136,35.07662"},{"tmc_status":"畅通","tmc_distance":"61","tmc_polyline":"118.344136,35.07662;118.34419,35.076067"},{"tmc_status":"畅通","tmc_distance":"40","tmc_polyline":"118.34419,35.076067;118.344227,35.075703"},{"tmc_status":"畅通","tmc_distance":"89","tmc_polyline":"118.344227,35.075703;118.344313,35.074903"},{"tmc_status":"畅通","tmc_distance":"14","tmc_polyline":"118.344313,35.074903;118.344329,35.074769"},{"tmc_status":"畅通","tmc_distance":"75","tmc_polyline":"118.344329,35.074769;118.344399,35.074093"},{"tmc_status":"畅通","tmc_distance":"84","tmc_polyline":"118.344399,35.074093;118.344474,35.073337"},{"tmc_status":"畅通","tmc_distance":"24","tmc_polyline":"118.344474,35.073337;118.344495,35.073122"},{"tmc_status":"畅通","tmc_distance":"39","tmc_polyline":"118.344495,35.073122;118.344528,35.072771"}],"navi":{"action":"","assistant_action":"到达途经地"},"polyline":"118.344055,35.07738;118.344071,35.077167;118.344093,35.077028;118.344115,35.076813;118.344136,35.07662;118.34419,35.076067;118.344227,35.075703;118.344313,35.074903;118.344329,35.074769;118.344399,35.074093;118.344474,35.073337;118.344495,35.073122;118.344528,35.072771"},{"instruction":"沿沂蒙路向南行驶474米到达途经地","orientation":"南","road_name":"沂蒙路","step_distance":"474","cost":{"duration":"93","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"1"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"83","tmc_polyline":"118.344528,35.072771;118.344597,35.072028"},{"tmc_status":"畅通","tmc_distance":"77","tmc_polyline":"118.344597,35.072028;118.344656,35.071336"},{"tmc_status":"畅通","tmc_distance":"97","tmc_polyline":"118.344656,35.071336;118.344683,35.070462"},{"tmc_status":"畅通","tmc_distance":"75","tmc_polyline":"118.344683,35.070462;118.34471,35.06978"},{"tmc_status":"畅通","tmc_distance":"142","tmc_polyline":"118.34471,35.06978;118.344715,35.069319;118.34469,35.068503"}],"navi":{"action":"","assistant_action":"到达途经地"},"polyline":"118.344528,35.072771;118.344597,35.072028;118.344656,35.071336;118.344683,35.070462;118.34471,35.06978;118.344715,35.069319;118.34469,35.068503"},{"instruction":"沿沂蒙路向南行驶336米到达途经地","orientation":"南","road_name":"沂蒙路","step_distance":"336","cost":{"duration":"68","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"1"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"7","tmc_polyline":"118.34469,35.068503;118.344688,35.068439"},{"tmc_status":"畅通","tmc_distance":"54","tmc_polyline":"118.344688,35.068439;118.344688,35.068359;118.344688,35.067946"},{"tmc_status":"畅通","tmc_distance":"78","tmc_polyline":"118.344688,35.067946;118.344699,35.067238"},{"tmc_status":"畅通","tmc_distance":"21","tmc_polyline":"118.344699,35.067238;118.344699,35.067039"},{"tmc_status":"畅通","tmc_distance":"55","tmc_polyline":"118.344699,35.067039;118.344705,35.066546"},{"tmc_status":"畅通","tmc_distance":"121","tmc_polyline":"118.344705,35.066546;118.344672,35.065811;118.344631,35.06546"}],"navi":{"action":"","assistant_action":"到达途经地"},"polyline":"118.34469,35.068503;118.344688,35.068439;118.344688,35.068359;118.344688,35.067946;118.344699,35.067238;118.344699,35.067039;118.344705,35.066546;118.344672,35.065811;118.344631,35.06546"},{"instruction":"沿沂蒙路向南行驶549米到达途经地","orientation":"南","road_name":"沂蒙路","step_distance":"549","cost":{"duration":"121","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"2"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"24","tmc_polyline":"118.344631,35.06546;118.344619,35.065355;118.344608,35.065247"},{"tmc_status":"畅通","tmc_distance":"85","tmc_polyline":"118.344608,35.065247;118.344528,35.06448"},{"tmc_status":"畅通","tmc_distance":"20","tmc_polyline":"118.344528,35.06448;118.344511,35.064303"},{"tmc_status":"畅通","tmc_distance":"33","tmc_polyline":"118.344511,35.064303;118.34449,35.06404;118.344485,35.063998"},{"tmc_status":"畅通","tmc_distance":"104","tmc_polyline":"118.344485,35.063998;118.344415,35.063112;118.34441,35.063059"},{"tmc_status":"畅通","tmc_distance":"134","tmc_polyline":"118.34441,35.063059;118.344345,35.062356;118.344318,35.061852"},{"tmc_status":"畅通","tmc_distance":"149","tmc_polyline":"118.344318,35.061852;118.344234,35.060517"}],"navi":{"action":"","assistant_action":"到达途经地"},"polyline":"118.344631,35.06546;118.344619,35.065355;118.344608,35.065247;118.344528,35.06448;118.344511,35.064303;118.34449,35.06404;118.344485,35.063998;118.344415,35.063112;118.34441,35.063059;118.344345,35.062356;118.344318,35.061852;118.344234,35.060517"},{"instruction":"沿沂蒙路向南行驶584米到达途经地","orientation":"南","road_name":"沂蒙路","step_distance":"584","cost":{"duration":"135","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"2"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"26","tmc_polyline":"118.344234,35.060517;118.344227,35.060403;118.344216,35.060291"},{"tmc_status":"畅通","tmc_distance":"48","tmc_polyline":"118.344216,35.060291;118.344173,35.059856"},{"tmc_status":"畅通","tmc_distance":"20","tmc_polyline":"118.344173,35.059856;118.344152,35.059674"},{"tmc_status":"畅通","tmc_distance":"27","tmc_polyline":"118.344152,35.059674;118.344136,35.059491;118.344125,35.059427"},{"tmc_status":"畅通","tmc_distance":"44","tmc_polyline":"118.344125,35.059427;118.344104,35.059105;118.344104,35.059035"},{"tmc_status":"畅通","tmc_distance":"33","tmc_polyline":"118.344104,35.059035;118.344093,35.058778;118.344088,35.058746"},{"tmc_status":"畅通","tmc_distance":"23","tmc_polyline":"118.344088,35.058746;118.344093,35.058601;118.344088,35.058531"},{"tmc_status":"畅通","tmc_distance":"41","tmc_polyline":"118.344088,35.058531;118.344061,35.058156"},{"tmc_status":"畅通","tmc_distance":"85","tmc_polyline":"118.344061,35.058156;118.34405,35.057448;118.34405,35.057383"},{"tmc_status":"畅通","tmc_distance":"125","tmc_polyline":"118.34405,35.057383;118.34405,35.057372;118.344136,35.056257"},{"tmc_status":"畅通","tmc_distance":"14","tmc_polyline":"118.344136,35.056257;118.344152,35.056123"},{"tmc_status":"畅通","tmc_distance":"98","tmc_polyline":"118.344152,35.056123;118.344217,35.055247"}],"navi":{"action":"","assistant_action":"到达途经地"},"polyline":"118.344234,35.060517;118.344227,35.060403;118.344216,35.060291;118.344173,35.059856;118.344152,35.059674;118.344136,35.059491;118.344125,35.059427;118.344104,35.059105;118.344104,35.059035;118.344093,35.058778;118.344088,35.058746;118.344093,35.058601;118.344088,35.058531;118.344061,35.058156;118.34405,35.057448;118.34405,35.057383;118.34405,35.057372;118.344136,35.056257;118.344152,35.056123;118.344217,35.055247"},{"instruction":"沿沂蒙路向南行驶929米到达途经地","orientation":"南","road_name":"沂蒙路","step_distance":"929","cost":{"duration":"188","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"2"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"7","tmc_polyline":"118.344217,35.055247;118.344222,35.055184"},{"tmc_status":"畅通","tmc_distance":"72","tmc_polyline":"118.344222,35.055184;118.344281,35.054529"},{"tmc_status":"畅通","tmc_distance":"80","tmc_polyline":"118.344281,35.054529;118.34434,35.053805"},{"tmc_status":"畅通","tmc_distance":"59","tmc_polyline":"118.34434,35.053805;118.344383,35.053269"},{"tmc_status":"畅通","tmc_distance":"57","tmc_polyline":"118.344383,35.053269;118.344426,35.052754"},{"tmc_status":"畅通","tmc_distance":"32","tmc_polyline":"118.344426,35.052754;118.344447,35.052459"},{"tmc_status":"畅通","tmc_distance":"102","tmc_polyline":"118.344447,35.052459;118.344506,35.051536"},{"tmc_status":"畅通","tmc_distance":"64","tmc_polyline":"118.344506,35.051536;118.344538,35.051107;118.344538,35.050956"},{"tmc_status":"畅通","tmc_distance":"39","tmc_polyline":"118.344538,35.050956;118.344528,35.050608"},{"tmc_status":"畅通","tmc_distance":"29","tmc_polyline":"118.344528,35.050608;118.344511,35.050334"},{"tmc_status":"畅通","tmc_distance":"27","tmc_polyline":"118.344511,35.050334;118.344501,35.050088"},{"tmc_status":"畅通","tmc_distance":"47","tmc_polyline":"118.344501,35.050088;118.344479,35.049664"},{"tmc_status":"畅通","tmc_distance":"26","tmc_polyline":"118.344479,35.049664;118.344474,35.049422"},{"tmc_status":"畅通","tmc_distance":"37","tmc_polyline":"118.344474,35.049422;118.344452,35.049085"},{"tmc_status":"畅通","tmc_distance":"41","tmc_polyline":"118.344452,35.049085;118.344436,35.048709"},{"tmc_status":"畅通","tmc_distance":"73","tmc_polyline":"118.344436,35.048709;118.344393,35.048049"},{"tmc_status":"畅通","tmc_distance":"137","tmc_polyline":"118.344393,35.048049;118.344331,35.046816"}],"navi":{"action":"","assistant_action":"到达途经地"},"polyline":"118.344217,35.055247;118.344222,35.055184;118.344281,35.054529;118.34434,35.053805;118.344383,35.053269;118.344426,35.052754;118.344447,35.052459;118.344506,35.051536;118.344538,35.051107;118.344538,35.050956;118.344528,35.050608;118.344511,35.050334;118.344501,35.050088;118.344479,35.049664;118.344474,35.049422;118.344452,35.049085;118.344436,35.048709;118.344393,35.048049;118.344331,35.046816"},{"instruction":"沿沂蒙路向南行驶598米到达途经地","orientation":"南","road_name":"沂蒙路","step_distance":"598","cost":{"duration":"109","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"2"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"6","tmc_polyline":"118.344331,35.046816;118.344329,35.046762"},{"tmc_status":"畅通","tmc_distance":"96","tmc_polyline":"118.344329,35.046762;118.344313,35.046386;118.344286,35.045898"},{"tmc_status":"畅通","tmc_distance":"28","tmc_polyline":"118.344286,35.045898;118.344275,35.04564"},{"tmc_status":"畅通","tmc_distance":"39","tmc_polyline":"118.344275,35.04564;118.344259,35.045286"},{"tmc_status":"畅通","tmc_distance":"112","tmc_polyline":"118.344259,35.045286;118.344216,35.044267"},{"tmc_status":"畅通","tmc_distance":"42","tmc_polyline":"118.344216,35.044267;118.3442,35.043881"},{"tmc_status":"畅通","tmc_distance":"16","tmc_polyline":"118.3442,35.043881;118.344195,35.043736"},{"tmc_status":"畅通","tmc_distance":"47","tmc_polyline":"118.344195,35.043736;118.344168,35.043307"},{"tmc_status":"畅通","tmc_distance":"45","tmc_polyline":"118.344168,35.043307;118.344115,35.042899"},{"tmc_status":"畅通","tmc_distance":"41","tmc_polyline":"118.344115,35.042899;118.34412,35.042524"},{"tmc_status":"畅通","tmc_distance":"8","tmc_polyline":"118.34412,35.042524;118.344109,35.042443"},{"tmc_status":"畅通","tmc_distance":"76","tmc_polyline":"118.344109,35.042443;118.344066,35.041751"},{"tmc_status":"畅通","tmc_distance":"42","tmc_polyline":"118.344066,35.041751;118.344042,35.041378"}],"navi":{"action":"","assistant_action":"到达途经地"},"polyline":"118.344331,35.046816;118.344329,35.046762;118.344313,35.046386;118.344286,35.045898;118.344275,35.04564;118.344259,35.045286;118.344216,35.044267;118.3442,35.043881;118.344195,35.043736;118.344168,35.043307;118.344115,35.042899;118.34412,35.042524;118.344109,35.042443;118.344066,35.041751;118.344042,35.041378"},{"instruction":"沿沂蒙路向南行驶24米向右前方行驶进入辅路","orientation":"南","road_name":"沂蒙路","step_distance":"24","cost":{"duration":"20","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"1"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"24","tmc_polyline":"118.344042,35.041378;118.344029,35.041161"}],"navi":{"action":"向右前方行驶","assistant_action":"进入辅路"},"polyline":"118.344042,35.041378;118.344029,35.041161"},{"instruction":"沿沂蒙路辅路向南行驶278米到达途经地","orientation":"南","road_name":"沂蒙路辅路","step_distance":"278","cost":{"duration":"60","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"0"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"84","tmc_polyline":"118.344029,35.041161;118.343911,35.040893;118.343884,35.040421"},{"tmc_status":"畅通","tmc_distance":"41","tmc_polyline":"118.343884,35.040421;118.343868,35.040051"},{"tmc_status":"畅通","tmc_distance":"26","tmc_polyline":"118.343868,35.040051;118.343857,35.039809"},{"tmc_status":"畅通","tmc_distance":"29","tmc_polyline":"118.343857,35.039809;118.343841,35.039546"},{"tmc_status":"畅通","tmc_distance":"98","tmc_polyline":"118.343841,35.039546;118.343797,35.038668"}],"navi":{"action":"","assistant_action":"到达途经地"},"polyline":"118.344029,35.041161;118.343911,35.040893;118.343884,35.040421;118.343868,35.040051;118.343857,35.039809;118.343841,35.039546;118.343797,35.038668"},{"instruction":"沿沂蒙路辅路向南行驶22米右转","orientation":"南","road_name":"沂蒙路辅路","step_distance":"22","cost":{"duration":"1","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"1"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"22","tmc_polyline":"118.343797,35.038668;118.343787,35.038468"}],"navi":{"action":"右转","assistant_action":""},"polyline":"118.343797,35.038668;118.343787,35.038468"},{"instruction":"沿陶然路向西行驶240米左转","orientation":"西","road_name":"陶然路","step_distance":"240","cost":{"duration":"59","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"1"},"tmcs":[{"tmc_status":"畅通","tmc_distance":"101","tmc_polyline":"118.343787,35.038468;118.342677,35.038388"},{"tmc_status":"畅通","tmc_distance":"70","tmc_polyline":"118.342677,35.038388;118.34191,35.03834"},{"tmc_status":"畅通","tmc_distance":"69","tmc_polyline":"118.34191,35.03834;118.341153,35.038286"}],"navi":{"action":"左转","assistant_action":""},"polyline":"118.343787,35.038468;118.342677,35.038388;118.34191,35.03834;118.341153,35.038286"},{"instruction":"沿八一路向东南行驶269米到达目的地","orientation":"东南","road_name":"八一路","step_distance":"269","cost":{"duration":"81","tolls":"0","toll_distance":"0","toll_road":"","traffic_lights":"0"},"tmcs":[{"tmc_status":"缓行","tmc_distance":"269","tmc_polyline":"118.340906,35.038146;118.341062,35.037374;118.3411,35.036998;118.341143,35.036896;118.341201,35.036853;118.34125,35.036843;118.341448,35.036837;118.342524,35.036894"}],"navi":{"action":"","assistant_action":"到达目的地"},"polyline":"118.340906,35.038146;118.341062,35.037374;118.3411,35.036998;118.341143,35.036896;118.341201,35.036853;118.34125,35.036843;118.341448,35.036837;118.342524,35.036894"}]}]}}';
//        $result = json_decode($result, true);
//        $json = json_encode($result, JSON_UNESCAPED_UNICODE);
        $paths = $result['route']['paths'][0];

        // 获取围栏内、围栏外距离
        $in_distance = 0; // 围栏内 总距离，单位：米
        $out_distance = 0; // 围栏外 总距离，单位：米
        $distance = $paths['distance']; // 总距离，单位：米
        $convert = new \convert\convert();
        $pts = [];
        $weilan_list = array_filter(explode('|', $freight['weilan']));
        foreach ($weilan_list as $val) {
            $arr = explode(',', $val);
            $pts[] = [
                'lng' => $arr[1],
                'lat' => $arr[0]
            ];
        }
        foreach ($paths['steps'] as $steps) {
            foreach ($steps['tmcs'] as $tmcs) {
                $tmc_polyline = explode(';', $tmcs['tmc_polyline']);
                $end_polyline = end($tmc_polyline);
                $end_polyline_data = explode(',', $end_polyline);
                $point = ['lng' => $end_polyline_data[0], 'lat' => $end_polyline_data[1]];
                $bool = $convert->is_point_in_polygon($point, $pts);
                if ($bool == true) {
                    $in_distance = bcadd($in_distance, $tmcs['tmc_distance']);
                } else {
                    $out_distance = bcadd($out_distance, $tmcs['tmc_distance']);
                }
            }
        }
        $in_distance = bcdiv($in_distance, 1000, 2); // 围栏内 总距离，单位：千米
        $out_distance = bcdiv($out_distance, 1000, 2); // 围栏外 总距离，单位：千米
        $distance = bcadd($in_distance, $out_distance, 2); // 总距离，单位：千米

        // 订单是否在围栏内
        $lng = $order['start_lng'];
        $lat = $order['start_lat'];
        $is_in = $convert->is_point_in_polygon(compact('lng', 'lat'), $pts);

        // 获取金额
        $in_fee = 0; // 围栏内 金额
        $out_fee = 0; // 围栏外 金额
        $in_surplus_fee = 0; // 围栏内 里程金额
        $out_surplus_fee = 0; // 围栏外 里程金额
        $in_start_price = $info['in_start_price']; // 围栏内-起步价
        $in_start_kl = $info['in_start_kl']; // 围栏内-起步公里数
        $is_in_start_kl = 0; // 是否在围栏内-起步公里数内 0=否,1=是
        $et_start_price = $info['et_start_price']; // 围栏外-起步价
        $et_start_kl = $info['et_start_kl']; // 围栏外-起步公里数
        $et_in_start_kl = 0; // 是否在围栏外-起步公里数内 0=否,1=是
        if ($is_in == true) {
            // 订单起点在围栏内
            if ($in_distance <= $in_start_kl) {
                $in_fee = $in_start_price;
                $is_in_start_kl = 1;
            }
            $start_fee = $in_start_price;
            $et_start_price = 0;
            $et_start_kl = 0;
        } else {
            // 订单起点在围栏外
            if ($out_distance <= $et_start_kl) {
                $out_fee = $et_start_price;
                $et_in_start_kl = 1;
            }
            $start_fee = $et_start_price;
            $in_start_price = 0;
            $in_start_kl = 0;
        }
        if (!$is_in_start_kl) {
            if ($in_distance > $info['in_protect_kl']) {
                $in_fee = $info['in_price_top'];
            } else {
                $in_distance_surplus = bcsub($in_distance, $in_start_kl, 2);
                $in_unit_price = 0;
                foreach ($info['in_exceed_rule_json_text'] as $key => $val) {
                    if ($in_distance_surplus <= $key) {
                        $in_unit_price = $val;
                        break;
                    }
                }
                $in_surplus_fee = bcmul($in_distance_surplus, $in_unit_price, 2);
                $in_fee = $in_start_price + $in_surplus_fee;
            }
        }
        if (!$et_in_start_kl) {
            $et_distance_surplus = bcsub($out_distance, $et_start_kl, 2);
            $et_unit_price = 0;
            foreach ($info['et_exceed_rule_json_text'] as $key => $val) {
                if ($et_distance_surplus <= $key) {
                    $et_unit_price = $val;
                    break;
                }
            }
            $out_surplus_fee = bcmul($et_distance_surplus, $et_unit_price, 2);
            $out_fee = $et_start_price + $out_surplus_fee;
        }
        $total_fee = bcadd($in_fee, $out_fee, 2);

        $oid = $order['id'];
        $total_time = bcsub(time(), strtotime($order['start_time']));

        return compact('oid', 'waypoints','waypoints_16', 'json', 'is_in', 'distance', 'in_distance', 'out_distance', 'total_fee', 'in_fee', 'out_fee', 'in_surplus_fee', 'out_surplus_fee', 'start_fee', 'total_time');
    }

}
